package net.csdn.business.auth.utils;

import cn.hutool.core.util.StrUtil;
import net.csdn.business.common.oauth2.model.LoginUser;
import net.csdn.business.auth.constant.SecurityConstants;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;


import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 权限获取工具类
 * 
 * @author zhangyl
 */
public class SecurityUtils
{
    /**
     * 获取用户ID
     */
    public static String getUserId()
    {
        LoginUser loginUser=getUser();
        if(loginUser!=null){
            return loginUser.getId();
        }
        return null;
    }

    /**
     * 获取用户名称
     */
    public static String getUsername()
    {
        LoginUser loginUser=getUser();
        if(loginUser!=null){
            return loginUser.getUsername();
        }
        return "system";
    }


    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户
     * @param authentication
     * @return LoginUser
     * <p>
     */
    public static LoginUser getUser(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof LoginUser) {
            return (LoginUser) principal;
        }
        return null;
    }

    /**
     * 获取用户
     */
    public static LoginUser getUser() {
        Authentication authentication = getAuthentication();
        if(authentication==null){
            return null;
        }
        return getUser(authentication);
    }

    /**
     * 获取用户角色信息
     * @return 角色集合
     */
    public static Set<String> getRoles() {
        Set<String> roles = new HashSet<>();
        Authentication authentication = getAuthentication();
        if(authentication==null){
            return roles;
        }
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        authorities.stream().filter(granted -> StrUtil.startWith(granted.getAuthority(), SecurityConstants.ROLE))
                .forEach(granted -> {
                    String role = StrUtil.removePrefix(granted.getAuthority(), SecurityConstants.ROLE);
                    roles.add(role);
                });
        return roles;
    }

    /**
     * 获取用户的权限信息
     * @return 角色集合
     */
    public static Set<String> getPermissions() {
        Set<String> permissions = new HashSet<>();
        Authentication authentication = getAuthentication();
        if(authentication==null){
            return permissions;
        }
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        authorities.stream().filter(granted -> !StrUtil.startWith(granted.getAuthority(), SecurityConstants.ROLE))
                .forEach(granted -> {
                    permissions.add(granted.getAuthority());
                });
        return permissions;
    }

}
